﻿
<MCol Cols="12" Md="6">
    <MCheckbox Label="显示名称与唯一名称保持一致" TValue="bool" Value="IsSame" ValueChanged="IsSameChanged" />
</MCol>
<MCol Cols="12" Md="6">
    <MTextField Value="Name" Color="@color" PersistentHint PrependIcon="@(color==null?null:"mdi-information-outline")" Hint="@(color==null?null:"该名称已存在，请检查是否已创建该词条，或考虑使用显示名称")" TValue="string" ValueChanged="OnNameChanged" Label="唯一名称" />
</MCol>
<MCol Cols="12" Md="6">
    <MTextField Value="DisplayName" Disabled="IsSame" TValue="string" ValueChanged="DisplayNameChanged" Label="显示名称" />
</MCol>
@code {
    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public string Name { get; set; }
    [Parameter]
    public EventCallback<string> NameChanged { get; set; }
    [Parameter]
    public string DisplayName { get; set; }
    [Parameter]
    public List<string> ExistingItems { get; set; } = new List<string>();
    [Parameter]
    public EventCallback<string> DisplayNameChanged { get; set; }

    private bool IsSame { get; set; }
    string color;

    protected override void OnInitialized()
    {
        IsSame = Name == DisplayName;
    }

    public async Task IsSameChanged(bool same)
    {
        IsSame = same;
        if (IsSame)
        {
            DisplayName = Name;
            await DisplayNameChanged.InvokeAsync(DisplayName);
            StateHasChanged();
        }
    }

    public async Task OnNameChanged(string name)
    {
        Name = name;
        await NameChanged.InvokeAsync(name);

        if (ExistingItems!=null&&ExistingItems.Any(s => s == name))
        {
            color = "error";
        }
        else
        {
            color = null;
        }

        if (IsSame)
        {
            DisplayName = name;
            await DisplayNameChanged.InvokeAsync(name);
        }
    }
}
